package com.woniuxy.stage3demo.controller.otherPzc;

import com.github.pagehelper.PageInfo;
import com.woniuxy.stage3demo.bean.ResultMsg;
import com.woniuxy.stage3demo.bean.otherPzc.AdsBean;
import com.woniuxy.stage3demo.bean.otherPzc.QainforBean;
import com.woniuxy.stage3demo.pageHelper.PageRequest;
import com.woniuxy.stage3demo.pageHelper.TableResult;
import com.woniuxy.stage3demo.service.otherPzc.IAdsService;
import com.woniuxy.stage3demo.service.otherPzc.IQainforService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RequestMapping("/ads")
@RestController
public class AdsController {

    @Resource
    private IAdsService adsServiceImpl;

    @RequestMapping(value = "/page",produces = {"application/json;charset=utf-8"})
    public TableResult<AdsBean> findAllByParams(PageRequest request, AdsBean adsBean){
        PageInfo<AdsBean> pageInfo = adsServiceImpl.findAllByParams(request, adsBean);
        return new TableResult<>(pageInfo.getTotal(),pageInfo.getList());
    }
    @PutMapping(value = "/status/{id}/{status}",produces = {"application/json;charset=utf-8"})
    public ResultMsg statusUpdate(@PathVariable("id") Long id,@PathVariable("status") Integer status){
        ResultMsg res = new ResultMsg(200,"操作成功",null);
        if(id != null && status != null) {
            AdsBean adsBean = new AdsBean();
            adsBean.setId(id);
            adsBean.setStatus(status);
            adsServiceImpl.updateAds(adsBean);
            return res;
        }else{
            res.setCode(10000);
            res.setMsg("更改失败");
            return res;
        }
    }
    @DeleteMapping(value = "/{id}",produces = {"application/json;charset=utf-8"})
    public ResultMsg qaInforDel(@PathVariable("id") Long id){
        ResultMsg res = new ResultMsg(200,"操作成功",null);
        if(id != null) {
            adsServiceImpl.deleteAds(id.intValue());
            return res;
        }else{
            res.setCode(10000);
            res.setMsg("更改失败");
            return res;
        }
    }
    @PutMapping(value = "/infor",produces = {"application/json;charset=utf-8"})
    public ResultMsg update(@RequestBody AdsBean adsBean){
        ResultMsg res = new ResultMsg(200,"操作成功",null);
        if(adsServiceImpl.updateAds(adsBean) == 1){
            return res;
        }else{
            res.setCode(10000);
            res.setMsg("修改失败");
            return res;
        }
    }
    @PutMapping(value = "/more",produces = {"application/json;charset=utf-8"})
    public ResultMsg save(@RequestBody AdsBean adsBean){
        adsBean.setAdsNo("ad"+new Date().getTime());
        ResultMsg res = new ResultMsg(200,"操作成功",null);
        if(adsServiceImpl.saveAds(adsBean) == 1){
            return res;
        }else{
            res.setCode(10000);
            res.setMsg("修改失败");
            return res;
        }
    }
    @PutMapping(value = "/delbyids",produces = {"application/json;charset=utf-8"})
    public ResultMsg delByIds(@RequestBody Long[] ids){
        ResultMsg res = new ResultMsg(200,"操作成功",null);
        if(ids != null) {
            for(int i = 0;i<ids.length;i++){
                adsServiceImpl.deleteAds(ids[i].intValue());
            }
            return res;
        }else{
            res.setCode(10000);
            res.setMsg("更改失败");
            return res;
        }
    }
}
